<?php
/**
 * @file tags.php
 * @author CaoQiCheng
 * @date 2017年2月8日 下午17:05:11
 * @brief
 */
namespace haibao\jiepai\business;

use haibao\jiepai\model\data\comment\CmsComment as CmsCommentModel;
use haibao\jiepai\data\Comment as CommentData;
use haibao\jiepai\model\filter\comment\CmsComment as CmsCommentFilter;
use haibao\frame\data\query\Condition;
use haibao\jiepai\model\data\comment\CmsCommentAudit as CmsCommentAuditModel;
use haibao\jiepai\model\filter\comment\CmsCommentAudit as CmsCommentAuditFilter;
use haibao\cms\business\control\PublicController;
use haibao\jiepai\model\filter\comment\CmsCommentReply as CmsCommentReplyFilter;
use haibao\jiepai\model\data\comment\CmsCommentReply as CmsCommentReplyModel;

class Comment extends \haibao\jiepai\business\Base
{
    /**
     * 评论与评论回复两种类型
     */
    const COMMENT_TYPE = 1;
    const COMMENT_REPLY_TYPE = 2;


    /**
     * 评论审核类型
     */
    public static $commentAduit = array(
        CmsCommentModel::AUDITSTATUS_NO => '未审核',
        CmsCommentModel::AUDITSTATUS_YES => '已通过',
        CmsCommentModel::AUDITSTATUS_NOPASSED => '未通过',
        CmsCommentModel::AUDITSTATUS_SENSITIVE => '敏感评论',
    );

    /**
     * 评论不通过类型
     */
    public static $commentAduitReason = array(
        CmsCommentAuditModel::CATEGORY_AD => '涉嫌广告',
        CmsCommentAuditModel::CATEGORY_FALSE => '言论不实 ',
        CmsCommentAuditModel::CATEGORY_RADICAL => '言论过激',
        CmsCommentAuditModel::CATEGORY_REPORT => '纠纷举报',
        CmsCommentAuditModel::CATEGORY_PROHIBITED => '网监违禁',
        CmsCommentAuditModel::CATEGORY_OTHER => '其他',
    );

    /**
     * 评论状态
     */
    public static $commentFlag = array(
        CmsCommentModel::FLAG_IS => '正常',
        CmsCommentModel::FLAG_DEL => '关闭',
    );

    /**
     * 添加审核数据
     */
    public static function saveCommentAduit($commentAduit)
    {
        if (!$commentAduit->TypeID) {
            throw new BusinessException('评论或回复id不能为空');
        }
        if ($commentAduit->Type == CmsCommentAuditModel::TYPE_COMMENT) {
            self::updateCommentForAduit($commentAduit->TypeID, $commentAduit->Status);
            self::addCommentAduit($commentAduit);
        } else {
            $data = new CommentData();

            if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
                $commentAduit->UserID = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
            } else {
                $commentAduit->UserID = 5;
            }
            $data->updateCommentReplyForAduit($commentAduit->TypeID, $commentAduit->Status, $commentAduit->UserID);
            self::addCommentAduit($commentAduit);
        }
        return true;
    }

    /**
     * 删除审核数据 type 1评论 type 2 回复
     */
    public static function delCommentAudit($commentId, $type = 1)
    {
        if (!$commentId) {
            throw new BusinessException('评论或回复id不能为空');
        }

        if ($type == CmsCommentAuditModel::TYPE_COMMENT) {
            self::delCommentForAudit($commentId);
            self::delCommentAuditById($commentId);
        } else {
            $data = new CommentData();
            $data->delReplyAudit($commentId);
        }
    }

    /**
     * 修改comment表
     */
    private static function delCommentForAudit($commentId)
    {
        $commentInfo = self::getCommentInfoById($commentId);
        $commentInfo->AuditStatus = 0;
        $commentInfo->AuditTime = '';
        $commentInfo->AuditUserID = '';
        $data = new CommentData();
        $data->save($commentInfo);
    }

    /**
     * 修改commentAudit表
     */
    private static function delCommentAuditById($commentId)
    {
        $data = new CommentData();
        $data->delCommentAudit($commentId);
    }

    /**
     * 加精
     */
    public static function addPoint($commentId, $type)
    {
        $data = new CommentData();
        $data->addPoint($commentId, $type);
    }

    /**
     * 获取回复的马甲用户
     */
    public static function getMajiaUser()
    {
        if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
            $userId = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
        } else {
            $userId = 5;
        }

        $data = new CommentData();
        return $data->getMajiaUser($userId);
    }

    /**
     * 回复
     */
    public static function saveCommentReply($replyModel)
    {
        if (!$replyModel->CommentID) {
            throw new BusinessException('评论id不能为空');
        }
        if (!$replyModel->UserID) {
            throw new BusinessException('评论用户id不能为空');
        }
        if (!$replyModel->Content) {
            throw new BusinessException('评论内容不能为空');
        }
        $replyModel->ReplyTime = new \DateTime();
        $replyModel->Status = 0;
        $replyModel->Flag = 0;
        $replyModel->AuditStatus = CmsCommentModel::AUDITSTATUS_NO;
        $data = new CommentData();

        return $data->saveReply($replyModel);

    }

    /**
     * 添加评论图片
     */
    public static function saveCommentPictures($replyId, $imageIdArr)
    {
        if (!$replyId) {
            throw new BusinessException('回复未保存!');
        }
        if (!$imageIdArr) {
            throw new BusinessException('图片错误');
        }

        $imageIdArr = explode(',', $imageIdArr);
        $data = new CommentData();
        return $data->saveCommentPictures($replyId, $imageIdArr);
    }


    /**
     * 增加审核数据
     */
    private static function addCommentAduit($commentAduit)
    {
        if ($commentAduit->Status == CmsCommentAuditModel::STATUS_OK) {
            $commentAduit->Category = 0;
            $commentAduit->Reason = "误报";
            $commentAduit->CreateTime = new \DateTime();
            if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
                $commentAduit->UserID = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
            } else {
                $commentAduit->UserID = 5;
            }
            $commentAduit->Flag = CmsCommentAuditModel::FLAG_IS;
        } elseif ($commentAduit->Status == CmsCommentAuditModel::STATUS_NO) {
            $commentAduit->CreateTime = new \DateTime();
            if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
                $commentAduit->UserID = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
            } else {
                $commentAduit->UserID = 5;
            }
            $commentAduit->Flag = CmsCommentAuditModel::FLAG_IS;
        }
        $data = new CommentData();
        $data->add($commentAduit);
    }

    /**
     * $checkType = 1 审核通过  $checkType =2 审核不通过
     * 评论表的审核状态修改
     */
    private static function updateCommentForAduit($commentId, $checkType = 1)
    {
        //修改评论表中的状态
        $commentModel = self::getCommentInfoById($commentId);
        if ($checkType == CmsCommentAuditModel::STATUS_OK) {
            $commentModel->AuditStatus = CmsCommentModel::AUDITSTATUS_YES;
        } elseif ($checkType == CmsCommentAuditModel::STATUS_NO) {
            $commentModel->AuditStatus = CmsCommentModel::AUDITSTATUS_NOPASSED;
        }
        $commentModel->AuditTime = new \DateTime();
        if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
            $commentModel->AuditUserID = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
        } else {
            $commentModel->AuditUserID = 5;
        }
        $data = new CommentData();
        $data->save($commentModel);
    }

    public static function getOneComment($query)
    {
        $data = new CommentData();
        return $data->getOne($query);
    }

    public static function getCommentList($query)
    {
        $data = new CommentData();
        return $data->getCommentList($query);
    }

    public static function getCommentInfoById($commentId)
    {
        $data = new CommentData();
        return $data->getOneById($commentId);
    }

    public static function checkCommentByIds($commentIds, $commentCheck = CmsCommentModel::AUDITSTATUS_YES, $commentAduitData = null)
    {
        if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
            $userId = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
        } else {
            $userId = 5;
        }
        if ($commentAduitData) {
            foreach ($commentIds as $key => $val) {
                $commentAduitData->TypeID = $val;
                $commentAduitData->Type = CmsCommentAuditModel::TYPE_COMMENT;
                self::addCommentAduit($commentAduitData);
            }
        }

        $data = new CommentData();
        $data->checkCommentByIds($commentIds, $userId, $commentCheck);

    }

    public static function checkReplyByIds($replyIdArr, $commentCheck = CmsCommentModel::AUDITSTATUS_YES, $commentAduitData = null)
    {
        if (in_array(\haibao\cms\business\base::getCurrentUser()->Id, array_keys(\haibao\jiepai\business\Base::$userMap))) {
            $userId = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
        } else {
            $userId = 5;
        }
        if ($commentAduitData) {
            foreach ($replyIdArr as $key => $val) {
                $commentAduitData->TypeID = $val;
                $commentAduitData->Type = CmsCommentAuditModel::TYPE_REPLY;
                self::addCommentAduit($commentAduitData);
            }
        }

        $data = new CommentData();
        $data->checkReplyByIds($replyIdArr, $userId, $commentCheck);

    }

    /**
     *    取得回复
     */
    public static function getCommentReplyById($replyId)
    {
        $commentReplyFilter = new CmsCommentReplyFilter();
        $commentReplyFilter->select(array(
            $commentReplyFilter::CONDITION_FIELD_NAME_ID,
            $commentReplyFilter::CONDITION_FIELD_NAME_AUDITSTATUS,
            $commentReplyFilter::CONDITION_FIELD_NAME_AUDITTIME,
            $commentReplyFilter::CONDITION_FIELD_NAME_AUDITUSERID,
            $commentReplyFilter::CONDITION_FIELD_NAME_COMMENTID,
            $commentReplyFilter::CONDITION_FIELD_NAME_CONTENT,
            $commentReplyFilter::CONDITION_FIELD_NAME_FLAG,
            $commentReplyFilter::CONDITION_FIELD_NAME_ISBEST,
            $commentReplyFilter::CONDITION_FIELD_NAME_REPLIEDID,
            $commentReplyFilter::CONDITION_FIELD_NAME_REPLIEDUSERID,
            $commentReplyFilter::CONDITION_FIELD_NAME_REPLYTIME,
            $commentReplyFilter::CONDITION_FIELD_NAME_SENSITIVEWORDS,
            $commentReplyFilter::CONDITION_FIELD_NAME_STATUS,
            $commentReplyFilter::CONDITION_FIELD_NAME_USERID
        ));
        $commentReplyFilter->where($commentReplyFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $replyId);
        $replyInfo = self::getOneComment($commentReplyFilter);
        return $replyInfo;
    }

    /**
     * 查询 comment 和commetn_reply表  sql语句查询
     */
    public static function getCommentListBySql($option)
    {
        $data = new CommentData();
        return $data->getCommentListBySql($option);
    }

    /**
     * @param $commentId 评论Id
     * @param $commentTpe 评论类型
     */
    public static function getCommentPictures($commentId, $commentType)
    {

        $data = new CommentData();
        return $data->getCommentPictures($commentId, $commentType);
    }

    /**
     * 获取评论举报审核信息
     * @param $commentId
     * @param $commentType
     */
    public static function getCommentAudit($commentId, $commentType)
    {

        $data = new CommentData();
        return $data->getCommentAudit($commentId, $commentType);
    }

    /**
     * 生成评论审核信息
     */
    public static function createCommentAudit($optionData)
    {

        $data = new CommentData();
        $data->createCommentAudit($optionData);
    }

    /**
     * 生成评论审核信息
     */
    public static function updateCommentAuditStatus($optionData)
    {

        $data = new CommentData();
        $data->updateCommentAuditStatus($optionData);
    }

    public static function updateCommentScores($commentId, $source)
    {
        $data = new CommentData();
        return $data->updateCommentScores($commentId, $source);
    }

    public static function pushPraiseMsg($commentIdArr, $type, $userId)
    {
        $data = new CommentData();
        return $data->pushPraiseMsg($commentIdArr, $type, $userId);
    }

    //删除我的评论
    public static function deleteMyComment($commentId, $userId, $type)
    {

        $data = new CommentData();
        return $data->deleteMyComment((int)$commentId, (int)$userId, $type);
    }

    //获取评论的回复信息
    public static function getReplyInfo($replyId)
    {

        $data = new CommentData();
        return $data->getReplyInfo((int)$replyId);
    }

    //获取我的评论
    public static function getMyComments($userId, $cursor, $version = null)
    {

        $data = new CommentData();
        return $data->getMyComments((int)$userId, $cursor, $version);
    }

    //获取评论内容及回复点赞数
    public static function getCommentInfo($commentId)
    {

        $data = new CommentData();
        $commentInfo = $data->getCommentInfo((int)$commentId);

        if ($commentInfo['AuditStatus'] == 1) {
            $commentInfo['Content'] = self::ReplaceSensitiveWords($commentInfo['Content'], $commentInfo['SensitiveWords']);
        }

        return $commentInfo;
    }

    public static function checkCommentReplyStatus($replyId)
    {
        $data = new CommentData();
        return $data->checkCommentReplyStatus($replyId);
    }

    public static function checkCommentStatus($commentId)
    {
        $data = new CommentData();
        return $data->checkCommentStatus($commentId);
    }

    public static function replyCommentArticle($param, $commentId)
    {
        $data = new CommentData();
        return $data->replyCommentArticle($param, $commentId);
    }

    public static function insertCommentArticle($params, $articleId, $ContentIndex)
    {
        $data = new CommentData();
        return $data->insertCommentArticle($params, $articleId, $ContentIndex);
    }

    public static function pushCommentMsg($typeId, $type, $fromUserID, $flag = null)
    {
        $data = new CommentData();
        return $data->pushCommentMsg($typeId, $type, $fromUserID, $flag);
    }

    public static function getCommentCountByImageIds($imageIds)
    {
        $imageIds = array_unique(array_filter($imageIds));
        $data = new CommentData();
        return $data->getCommentCount($imageIds);
    }

    public static function insertCommentPictures($pictureIdArr, $type, $typeId)
    {
        $data = new CommentData();
        return $data->insertCommentPictures($pictureIdArr, $type, $typeId);
    }

    /**
     * 回复评论或回复回复的评论
     */
    public static function replyComment($commentData)
    {
        $data = new CommentData();
        return $data->replyComment($commentData);
    }

    /**
     * 保存评论
     */
    public static function insertComment($commentData)
    {
        $data = new CommentData();
        return $data->saveComment($commentData);
    }

    public static function UpdateCommentReplySensitive($type, $typeId, $sensitiveWords)
    {
        if (!$sensitiveWords) {
            return false;
        }
        $data = new CommentData();
        return $data->UpdateCommentReplySensitive($type, $typeId, $sensitiveWords);
    }

    /**
     * 获取所有快速评论列表
     */
    public static function getAllQuickCommentList()
    {
        $data = new CommentData();
        return $data->getAllQuickCommentList();
    }

    /**
     * 快速评论列表（10个）
     */
    public static function quickCommentList()
    {
        $data = new CommentData();
        return $data->quickCommentList();
    }

    /**
     * 保存快速评论
     */
    public static function saveQuickComment($content)
    {
        if(!$content){
            throw new BusinessException('快速评论内容不能为空');
        }
        $data = new CommentData();
        return $data->saveQucikComment($content);
    }

    /**
     * 删除快速评论
     */
    public static function deleteQuickComment($quickId)
    {
        if(!$quickId){
            throw new BusinessException('参数错误');
        }
        $data = new CommentData();
        $data->deleteQuickComment($quickId);
    }
}